package com.example.demo.service;

import com.example.demo.mapper.RoleMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.Role;
import com.example.demo.pojo.User;
import com.example.demo.pojo.UserExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;


@Repository
public class UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;

    //获取用户所有数据
    public List<User> getAllUsers(){
        UserExample userExample = new UserExample();

        List<User> users = userMapper.selectByExample(userExample);
        for(User user : users) {
            System.out.println(user);
        }

        return users;

    }


    //通过用户名和密码查询用户
    public User getUserByLoginAndPwd(String loginName,String passWord){
        UserExample userExample = new UserExample();

        //创建多条件容器
        UserExample.Criteria criteria = userExample.createCriteria();
        //添加条件
        criteria.andU_loginNameEqualTo(loginName);
        criteria.andU_passWordEqualTo(passWord);

        List<User> users = userMapper.selectByExample(userExample);

        if (users.size() >=1 ){

            return users.get(0);
        }

        return null;
    }

    //获取用户首页展示数据
    public PageInfo<User> findInfoByName(Integer pageNum, Integer pageSize, String name){

        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if (!"".equals(name.trim()) && name.trim() != null){
            criteria.andU_trueNameLike("%"+name.trim()+"%");
        }

        //分页条件,sql和分页代码必须一起
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userMapper.selectByExample(example);

        if(users != null){
            return new PageInfo<>(users);
        }

        return null;
    }

    //密码管理，更新用户密码
    public int updatePassword(User newUser,int userId){

        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        //更新条件，通过用户id更新密码
        criteria.andU_idEqualTo(userId);

        return userMapper.updateByExample(newUser,userExample);
    }

    //获取角色列表
    public List<Role> getRoleList(){

        return roleMapper.getRoleList();
    }

    //新增用户信息
    public int addUser(User user){

        return userMapper.insert(user);
    }

    public List<User> checkByloginName(String loginName){

        UserExample example = new UserExample();
        //条件查询
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andU_loginNameEqualTo(loginName);

        return userMapper.selectByExample(example);
    }

    public List<Object> getUserInfoById(int userId){

        List<Object> objects = new ArrayList<>();
        User user = userMapper.selectByPrimaryKey(userId);
        if (user != null){
            int roleId = user.getR_id();
            Role role = roleMapper.selectByPrimaryKey(roleId);
            if (role !=null){
                objects.add(user);
                objects.add(role);
            }

        }

        return objects;
    }

    //更新用户信息
    public int updateUser(int userId,User user){

        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        //更新条件
        criteria.andU_idEqualTo(userId);

        return userMapper.updateByExampleSelective(user,example);

    }

    //删除键用户信息
    public int delete(int userId){
        return userMapper.deleteByPrimaryKey(userId);
    }

    //通过id获取role信息
    public Role getRoleInfoByRoleId(int roleId){

        return roleMapper.selectByPrimaryKey(roleId);
    }


}
